
using System;
using System.IO;
using Gecko;
using Gtk;
using System.Collections;
using System.Collections.Generic;
using IrrlichtNETCP;

namespace kinematix
{
	
	public class kHTMLView
	{
		kApplication app;
		WebControl moz;
		kPythonInterpreter python;
		Gtk.Window win;
		
	
		public kHTMLView(kApplication _app)
		{
			app = _app;
			python = app.PythonEngine;
			moz = new WebControl();
			moz.LoadUrl("file:///"+app.CurrentDir+"/labs/index.html");
			moz.OpenUri += OnOpenUri;
			win = new Window("kinematix");
			win.Resize(600,800);
			
			win.Add(moz);
			
			win.DeleteEvent += new DeleteEventHandler(OnDelete);
			
			win.ShowAll();
			
		}
		
		public void OnOpenUri(object o, OpenUriArgs args)
		{
			//Console.WriteLine(args.AURI);
			string str = args.AURI; 
			if (str.Contains("lab:///"))
			{
				
				str = str.Remove(0, 7);
				Console.WriteLine(str);
				(o as WebControl).LoadUrl("file:///"+app.CurrentDir+"/labs/"+str+"/index.html");
			try{
					StreamReader r = File.OpenText(app.CurrentDir+"/labs/"+str+"/main.py");
					python.Run(r.ReadToEnd());
			}
			catch ( Exception exc)
			{
			}
			args.RetVal = true;				
			}
			
			if (str.Contains("edit:///"))
			{
				str = str.Remove(0, 8);
				kPythonEditor ed = new kPythonEditor(app);
				ed.LoadFile(app.CurrentDir+"/labs/"+str+"/main.py");
			args.RetVal = true;				
			}
			
		}
		
		public void OnDelete(object o, DeleteEventArgs args)
		{
			app.Quit();
		}
		
	};
	
}
